package com.jeejam.cart.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jeejam.cart.dao.CartItemMapper;
import com.jeejam.cart.pojo.CartItem;
import com.jeejam.cart.service.CartItemService;
import com.jeejam.framework.base.service.impl.BaseServiceImpl;

@Service
public class CartItemServiceImpl extends BaseServiceImpl implements CartItemService{

	private CartItemMapper cartItemMapper;
	
	@Resource
	public void setCartItemMapper(CartItemMapper cartItemMapper) {
		this.cartItemMapper = cartItemMapper;
		this.baseMapper = cartItemMapper;
	}

	@Override
	public CartItem findByProductIdAndCartId(int productId, int cartId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("productId", productId);
		params.put("cartId", cartId);
		return cartItemMapper.selectByProductIdAndCartId(params);
	}

	@Override
	public void delete(int cartId) {
		cartItemMapper.deleteByCartId(cartId);
	}

	@Override
	public void delete(int productId, int cartId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("productId", productId);
		params.put("cartId", cartId);
		cartItemMapper.deleteByProductIdAndCartId(params);
	}

	@Override
	public void updateCount(int id, int count) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		params.put("count", count);
		cartItemMapper.updateCount(params);
	}

	@Override
	public void updateCount(CartItem item) {
		this.updateCount(item.getId(), item.getCount());
	}

	@Override
	public void update(int id, int count, int state) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		params.put("count", count);
		params.put("state", state);
		cartItemMapper.updateCountAndState(params);
	}

	@Override
	public void updateState(int id, int state) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		params.put("state", state);
		cartItemMapper.updateState(params);
	}

	@Override
	public void updateState(CartItem item) {
		this.updateState(item.getId(), item.getState());
	}

	@Override
	public List<CartItem> findForConfirmByCartId(int cartId) {
		return cartItemMapper.selectForConfirmByCartId(cartId);
	}

}
